package io.ooad.jenkins.shuren;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * OkHttp认证拦截器。从AuthHolder获取Access token，设置为HTTP Request的Authorization Header的值。
 * Created by yyang on 2017/1/2.
 */
public class AuthInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        String authHeader = AuthHolder.get();
        if (authHeader == null || authHeader.trim().isEmpty()) {
            return chain.proceed(chain.request());
        }
        Request newRequest = chain
                .request()
                .newBuilder()
                .header("Authorization", authHeader)
                .build();
        return chain.proceed(newRequest);
    }
}
